/*
 * Ethernet.c
 *
 * Created: 12/22/2011 8:52:47 AM
 *  Author: Administrator
 */ 

#include <avr/io.h>
#include <string.h>
#include <stdio.h>
#include <util/delay.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>

#include "ethernet.h"


// Debugging Mode, 0 - Debug OFF, 1 - Debug ON
#define _DEBUG_MODE      0

#define BAUD_RATE 19200

// Define W5100 Socket Register and Variables Used
const char *html_page= "<html><title>Embedded Web Server</title><body><h1>Embedded Web Server</h1><h2>Team members: A, B, C, D, E</h2><form name=\"myform\" method=\"POST\"><ul>LED 1:<input type=\"radio\" name=\"LED1\" value=\"ON\">ON<input type=\"radio\" name=\"LED1\" value=\"OFF\" checked>OFF</ul><ul>LED 2:<input type=\"radio\" name=\"LED2\" value=\"ON\">ON<input type=\"radio\" name=\"LED2\" value=\"OFF\" checked>OFF</ul><ul>LED 3:<input type=\"radio\" name=\"LED3\" value=\"ON\">ON<input type=\"radio\" name=\"LED3\" value=\"OFF\" checked>OFF</ul><ul>LED 4:<input type=\"radio\" name=\"LED4\" value=\"ON\">ON<input type=\"radio\" name=\"LED4\" value=\"OFF\" checked>OFF</ul><input type=\"submit\" value=\"Submit\" /> </form></body></html>";
uint8_t sockreg;
int tempvalue;
uint8_t ledmode,ledeye,ledsign;


int strindex(char *s,char *t)
{
	// this method returns the index of s if found in string t
	// otherwise it returns -1
}

int main(void)
{
	DDRD = 0xFF;// set port d as output with initial value of zeros

	uart_init();// Initialize UART Peripheral
  
	ansi_init();
  
	printf("\033[2J");// Clear the Screen of the hyper terminal <send clear code>
  
	spi_init();// Initialize SPI

	w5100_init();// Initial the W5100 Ethernet
  
	printf("EWS v0.1\n");
	
  // Loop forever
  while(1)
  {
	  printf("Inside while\n");
	  // keep reading the socket zero status
	  // and handle at least socket closed and socket established
	  uint8_t *rcvbuf;
	  uint8_t *sndbuf;
	 uint8_t sockstat; 
	sockreg=wiznet_read(S0_SR);
	switch(sockreg)/*Handle all possible socket status here*/ 
	{
	case SOCK_CLOSED:
		if(socket(MR_TCP,80)==0x01)
			printf("socket created\n");
		else
			printf("socket not initialized\n");
		if(listen()==0x01)
		printf("listening...\n");
		_delay_ms(1);
		//printf("\%x\n",wiznet_read(S0_SR));
		break;
	case SOCK_ESTABLISHED:
		// Get the client request size 
		printf("Before Receive\n");
		
		if(recv(buf,recv_size())==0x0001)// And read the client Request in temp buffer
			printf("received %s \n",buf);// make sure the temp buffer is sending GET request  PARSE TMP BUFFER
		
		if(send((uint8_t *)html_page,strlen(html_page)) == 0x0001)// create a buffer that will hold the response and send it
			printf("sent\n");// if the GET request has an option to turn on/off a LED Carry that now.
		wiznet_write(S0_CR,CR_DISCON);// finally disconnect the socket with the client.
		while (wiznet_read(S0_CR)!=0x00) {};
		break;
      case SOCK_FIN_WAIT:
      case SOCK_CLOSING:
      case SOCK_TIME_WAIT:
      case SOCK_CLOSE_WAIT:
      case SOCK_LAST_ACK:
		wiznet_write(S0_CR,CR_CLOSE);// force the socket to be closed
		printf("Last Ack\n");
		break;
	default:
		break;
	}	
		
  }
  return 0;
}